package com.zw.pdm.module.est.controller;

import com.zw.pdm.annotation.RepeatCommit;
import com.zw.pdm.base.BaseController;
import com.zw.pdm.common.JsonResult;
import com.zw.pdm.entity.est.Card;
import com.zw.pdm.module.basic.service.DictCommonService;
import com.zw.pdm.module.est.service.CardListService;
import com.zw.pdm.module.est.service.ChaWarnService;
import com.zw.pdm.module.report.service.SheetService;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import static com.zw.pdm.constant.Constant.*;

/**
 * @Description: 工艺卡列表
 *
 * @Author: China.sgl
 * @Date: 2020/8/3 16:11
 */
@Controller
@RequestMapping("/est/cardList")
@SuppressWarnings("all")
public class CardListController extends BaseController {

    @Autowired
    private CardListService cardListService;
    @Autowired
    private DictCommonService dictCommonService;
    @Autowired
    private SheetService sheetService;
    @Autowired
    private ChaWarnService chaWarnService;


    @GetMapping
    @RequiresPermissions(value = {"est:cardList:view"}, logical = Logical.OR)
    public String page(Model model) {
        model.addAttribute("specials", dictCommonService.findDictByGroupName(GROUP_SPECTYPE));
        return "est/cardList/page";
    }

    /** 列表 */
    @ResponseBody
    @PostMapping("/list")
    public JsonResult cardList(Card card) {
        startPage();
        List<Card> list = cardListService.findWithCardList(card);
        return jsonResult(list);
    }

    /** 查看  */
    @GetMapping("/{id}")
    public String info(@PathVariable("id") Integer id, Model model) {
        model.addAttribute("card", cardListService.getCardById(id));
        model.addAttribute("details", cardListService.findCardDetailByCardId(id));
        return "est/cardList/print";
    }

    /** 查看  */
    @GetMapping("/full/{id}")
    public String print(@PathVariable("id") Integer id, Model model) {
        model.addAttribute("card", cardListService.getCardById(id));
        model.addAttribute("details", cardListService.findCardDetailByCardId(id));
        return "est/cardList/full";
    }

    /** 删除 */
    @ResponseBody
    @PostMapping("/del")
    @RequiresPermissions({"est:cardList:del"})
    public JsonResult delCard(Integer id) {
        return jsonResult(cardListService.delCardById(id));
    }

    /** 提交 */
    @ResponseBody
    @RepeatCommit
    @PostMapping("/commit")
    @RequiresPermissions({"est:cardList:commit"})
    public JsonResult commitCard(Integer id) {
        return jsonResult(cardListService.commitCardById(id));
    }

    /** 提交 */
    @ResponseBody
    @RepeatCommit
    @PostMapping("/commitAll")
    @RequiresPermissions({"est:cardList:commit"})
    public JsonResult commitCard(Integer[] ids) {
        return jsonResult(cardListService.commitCardByIds(ids));
    }

    /** 修改页面 */
    @GetMapping("/edit/{id}")
    @RequiresPermissions({"est:cardList:edit"})
    public String editCard(@PathVariable("id") Integer id, Model model) {
        Card card = cardListService.getCardById(id);
        model.addAttribute("card", card);
//        model.addAttribute("details", cardListService.findCardDetailByCardId(id));//原查询工艺卡明细所用；
        model.addAttribute("details",chaWarnService.findCardDetailAndTranByCardId(id));//因为转序变更后编辑页面不允许修改路线调整；
        model.addAttribute("specials", dictCommonService.findDictByGroupName(GROUP_SPECTYPE));
        model.addAttribute("parts", sheetService.findPartByProjectCode(card.getProCode(), "外观".equals(card.getSpecial()) ? null: card.getSpecial()));
        model.addAttribute("drawNos", sheetService.findDrawNoByProCodeAndPartAndId(card.getProCode(), card.getPartId(), card.getCraMakeDetailId()));
        model.addAttribute("routes", dictCommonService.findRoutes());
        model.addAttribute("processes", dictCommonService.findProcesses());
        model.addAttribute("equNames", dictCommonService.findEqu());
        model.addAttribute("verifys", dictCommonService.findPersonByCategory("工艺员"));
        return "est/cardList/edit";
    }

    /** 删除明细 */
    @ResponseBody
    @PostMapping("/detail/del")
    public JsonResult delDetail(Integer id) {
        return jsonResult(cardListService.delDetailById(id));
    }

    /** 保存工艺卡 */
    @ResponseBody
    @PostMapping("/update")
    @RequiresPermissions(value = {"est:cardList:edit"}, logical = Logical.OR)
    public JsonResult update(Card card, Integer[] detailId, String[] route, Integer[] orderNo, String[] process, String[] equName, String[] craftContent) {
        return jsonResult(cardListService.update(card, detailId, route, orderNo, process, equName, craftContent));
    }

}
